window.debugTimeout = 0
window.debugCount = 0
window.isDebug = false

initDebugBtn()

function initDebugBtn () {
  var debugEl = document.createElement('div')
  debugEl.style.position = 'absolute'
  debugEl.style.width = '110px'
  debugEl.style.height = '50px'
  debugEl.style.top = '0'
  debugEl.style.left = '0'
  debugEl.style.zIndex = '2000'
  debugEl.onclick = debug
  document.addEventListener('DOMContentLoaded', function () {
    document.body.appendChild(debugEl)
  }, false)
}

function debug () {
  if (window.isDebug) return
  window.debugCount++
  if (window.debugCount === 10) openDebug()
  resetDebugTimeout()
}

function resetDebugTimeout () {
  clearTimeout(window.debugTimeout)
  window.debugTimeout = setTimeout(function () {
    window.debugTimeout = 0
    window.debugCount = 0
  }, 400)
}

function openDebug () {
  window.isDebug = true
  var scriptEl = document.createElement('script')
  scriptEl.src = '//cdn.jsdelivr.net/npm/eruda'
  scriptEl.onload = function () {
    eruda.init()
    console.log('eruda init')
    if (window.buildDate) console.log('build-date=' + window.buildDate)
  }
  document.body.appendChild(scriptEl)
}